package com.wangxq.loginmodule.yd_login.login.mvp.presenter;

import android.app.Application;
import android.util.Log;

import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.jess.arms.base.delegate.sys.LoginBean;
import com.jess.arms.base.delegate.sys.LoginReponsBody;
import com.jess.arms.base.delegate.sys.LoginStateUtil;
import com.jess.arms.base.delegate.sys.SysInfo;
import com.jess.arms.database.MMkvHelper;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxUtils;
import com.wangxq.loginmodule.yd_login.login.LoginApi;
import com.wangxq.loginmodule.yd_login.login.mvp.contract.LoginContract;
import com.zlx.module_base.sys_info.BaseResponsBean;

import java.io.Serializable;
import java.util.ArrayList;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;


@ActivityScope
public class LoginPresenter extends BasePresenter<LoginContract.Model, LoginContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    @Inject
    IRepositoryManager mRepositoryManager;
    @Inject
    public LoginPresenter (LoginContract.Model model, LoginContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
    public void Login(String url,String username,String password,boolean isSave){
        mModel.Login(url,username,password)
//                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new Observer<BaseResponsBean<LoginReponsBody>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponsBean<LoginReponsBody> responseBody) {
                        mRootView.hideLoading();
                        if(!ObjectUtils.isEmpty(responseBody)){
                            if(responseBody.isSuccess()){
                                if(responseBody.getRoot()!=null){
                                    SysInfo.emp = responseBody.getRoot();
//                            SysInfo.org = responseBody.getData().getOrg();
                                    CacheDiskUtils.getInstance().put("cache_psersoninfo", (Serializable) SysInfo.emp);
                                    MMkvHelper.getInstance().saveString("userLoginBody",new Gson().toJson(responseBody.getRoot()));
                                    LoginBean loginBean=new LoginBean();
                                    loginBean.uName=username;
                                    loginBean.pwd=password;
                                    CacheDiskUtils.getInstance().put("uNameAndPwd", new Gson().toJson(loginBean));
                                }

                                ToastUtils.showShort("登录成功！");
                                if(isSave){
                                    ArrayList<String> list = (ArrayList<String>)CacheDiskUtils.getInstance().getSerializable("users");
                                    if(list==null){
                                        list = new ArrayList<>();
                                    }
                                    if(!list.contains(username)){
                                        list.add(username);
                                        CacheDiskUtils.getInstance().put("users",list);
                                    }
                                }
                                mRootView.toMainActivity();
                            }else {
                                SysInfo.cookieStore.clear();
                                LoginStateUtil.clearOkhttpCookieCache();
                                if(!StringUtils.isTrimEmpty(responseBody.getMsg())){
                                    ToastUtils.showShort(responseBody.getMsg());
                                }else {
                                    ToastUtils.showShort("登录失败请重试！");
                                }
                            }
                        }else {
                            SysInfo.cookieStore.clear();
                            ToastUtils.showShort("连接服务器失败，请重试");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        SysInfo.cookieStore.clear();
                        mRootView.hideLoading();
                        ToastUtils.showShort("登录失败请重试！"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void test(){
        mRepositoryManager.obtainRetrofitService(LoginApi.class)
                .getSystem("http://139.9.246.226:8081/DTCG/pda/wbMd/getSystems.action")
                .subscribeOn(Schedulers.io())
                .compose(RxUtils.applySchedulersNoLoading(mRootView))
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
                        Log.e("返回信息",s+">>>");
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
